package com.example.lenovo.a203_yhy_ordering_3.model;

import com.example.lenovo.a203_yhy_ordering_3.beans.FoodCommentBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.FoodInfoBean;
import com.example.lenovo.a203_yhy_ordering_3.iface.FoodCommentIface;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodCommentListener;
import com.example.lenovo.a203_yhy_ordering_3.service.FoodCommentService;
import com.example.lenovo.a203_yhy_ordering_3.service.FoodInfoService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2018/12/2.
 */

public class FoodCommentModel implements FoodCommentIface{
    private Retrofit retrofit;

    public FoodCommentModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }



    @Override
    public void getFoodCommentResult(int food_id, final FoodCommentListener foodCommentListener) {
        FoodCommentService foodCommentService = retrofit.create(FoodCommentService.class);

        Call<List<FoodCommentBean>> foodCommentServiceFoodById = foodCommentService.getAllCommentsByFood(food_id);

        foodCommentServiceFoodById.enqueue(new Callback<List<FoodCommentBean>>() {
            @Override
            public void onResponse(Call<List<FoodCommentBean>> call, Response<List<FoodCommentBean>> response) {
                if(response!=null)
                    foodCommentListener.onResponse(response.body());
                else
                    foodCommentListener.onFail("获取评论失败");
            }

            @Override
            public void onFailure(Call<List<FoodCommentBean>> call, Throwable t) {
                foodCommentListener.onFail(t.toString());
            }
        });
    }
}
